<?php
/**
 * 
 * 公共工具类
 * @author ChunYang.Jiang<chunyang@staff.sina.com.cn>
 * @copyright sina
 * @date 2011-04-09
 *
 */
class Util
{
	/**
	 * 
	 * 没有什么用，防止当前类被实例化
	 */
	private function __construct(){}
		/**
	 * 表单接收参数过滤,不支持HTML参数过滤
	 * @param unknown_type $varName 接收参数的KEY名
	 * @param unknown_type $strlen  接收参数的长度限制，0为不受限制
	 * @param unknown_type $med   参数的提交类型 支持参数post，get
	 * @param unknown_type $type  参数类型，支持str,int
	 * @param unknown_type $isSql  是否进行mysql字符串转义
	 * @param unknown_type $isClearSpace  是否清除字符里的所有空格，防止sql注入,字符串不允许有空格时才用
	 */
	public static function varFilter ($varName, $med = "post", $type = "str", $minLen=1, $maxLen=200) 
	{
		$val = '';
		if ($med == "post") {
			$val = isset($_POST[$varName]) ? $_POST[$varName] : '';
		}
		else {
			$val = isset($_GET[$varName]) ? $_GET[$varName] : '';
		}
		$val = trim($val);
		$strLen = strlen($val);
		if ($type=='str'){
			if ($strLen<$minLen || $strLen>$maxLen){
				return FALSE;
			}
			if (!preg_match('/^\w$/', $val)){
				return FALSE;
			}
		}
	}
	
	/**
	 * 
	 * 返回模板引擎
	 */
	public static function getTpl($app='Page')
	{
		$tpl = new Smarty();
		$tpl->template_dir = ROOT_DIR . 'Templates/' . $app;
		$tpl->cache_dir = TMP_PATH;
		$tpl->config_dir = TMP_PATH;
		$tpl->compile_dir = TMP_PATH;
		$tpl->left_delimiter = '<{';
		$tpl->right_delimiter = '}>';
		return $tpl;
	}
	
	/**
	 * 返回编辑器
	 */
	public static function getFck($instance)
	{
		require_once ROOT_DIR . 'Libraries/fckeditor/fckeditor.php';
		$fck = new FCKeditor($instance);
		$fck->BasePath = APP_HOST . 'Libraries/fckeditor/';
		$fck->Width = 800;
		$fck->Height = 200;
		$fck->ToolbarSet = 'Basic';
		
		return $fck;
	}
	
	/**
	 * 
	 * 取得hash值
	 * @param hash $data 待hash变量
	 * @param unknown_type hash基数
	 */
	public static function hashData($data, $num)
	{
		$hash = sprintf('%u', crc32($data));
		$hash = intval($hash / $num);
		$hash = intval($hash % $num);
		$hash = dechex($hash);
		return sprintf('%02s', $hash);
	}
	
	/**
	 * 设置cookies
	 * @param string $key
	 * @param unknow $value
	 */
	public static function setCookies($key, $value)
	{
		$value = serialize($value);
		setcookie($key, $value, time() + COOKIE_EXPIRE, '/');
	}
	
	/**
	 * 返回cookies
	 * @param string $key
	 */
	public static function getCookies($key)
	{
		if (isset($_COOKIE[$key])){
			return unserialize($_COOKIE[$key]);
		}
		return false;
	}
	
	/**
	 * 清空cookies
	 * @param string $key
	 */
	public static function clearCookies($key)
	{
		setcookie($key, '', time()-1, '/');
	}
	
	/**
	 * 页面跳转
	 * @param 目标 $url
	 */
	public static function redirect($url = 'http://weibo.com') {
		@header ( "HTTP/1.1 302 Moved Temporarily" );
		@header ( "Status: 302 Moved Temporarily" );
		@header ( "Location: " . $url );
		exit ();
	}
	
	/**
	 * 操作提示
	 * @param 提示消息 $msg
	 * @param 跳转URL
	 * @param 等待时间
	 */
	public static function msgRedirect($msg, $url='-1', $time=3)
	{
		$tpl = self::getTpl('System');
		$tpl->assign('msg', $msg);
		$tpl->assign('jumpUrl', $url);
		$tpl->assign('time', $time);
		$tpl->display('msgRedirect.html');
		exit();
	}
	
	/**
	 * 错误日志sae_debug
	 * @param $log 错误消息
	 */
	public static function log($log)
	{
		sae_debug($log);
		return;
	}
	
	/**
	 * 生成分页代码
	 * @param $total 总记录数
	 * @param $pageSize 每页记录数
	 * @param $tpl tpl句柄
	 */
	public static function page($total, $pageSize, $tpl=null)
	{
		$page = isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page']>0
				? intval($_GET['page'])
				: 1;
		global $appName;
		global $ctrName;
		global $mtdName;
		$url = $appName != 'Page' ? "/$appName" : "";
		$url .= "/$ctrName/$mtdName/";
		foreach ($_GET as $name=>$value){
			if ($name=='page') continue;
			$url .= "$name/$value/";
		}
		
		$pageTotal = ceil($total/$pageSize);
		
		$prePage = $page>1 ? ($page-1) : 1;
		$nextPage = $page<$pageTotal ? ($page+1) : $pageTotal;
		
		$pageBreak = "共{$total}条，第{$page}/{$pageTotal}页&nbsp;&nbsp;";
		if ($total<$pageSize){
			;
		}else{
			$pageBreak .= $page>1 ? "<a href='{$url}page/1'><<</a>&nbsp;&nbsp;" : '';
			$pageBreak .= $page>1 ? "<a href='{$url}page/{$prePage}'><</a> " : '';
			for ($i=1; $i<=$pageTotal; $i++){
				$pageBreak .= $i==$page ? "<a href='{$url}page/{$i}' style='font-weight:bold;color:#F00'>{$i}</a> " : "<a href='{$url}page/{$i}'>{$i}</a> ";
			}
			$pageBreak .= $page<$pageTotal ? "<a href='{$url}page/$nextPage'>></a>&nbsp;&nbsp;" : '';
			$pageBreak .= $page<$pageTotal ? "<a href='{$url}page/$pageTotal'>>></a>" : '';
		}
		if($tpl){
			$tpl->assign('pageBreak', $pageBreak);
		}
		return $pageBreak;
	}
}